<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="模板名称" prop="surveyName">
          <el-input style="width: 200px" v-model="queryParams.surveyName" placeholder="请输入模板名称" clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
  
        <el-form-item label="模板类型" prop="surveyType">
          <el-select style="width: 200px" v-model="queryParams.surveyType" placeholder="请选择模板类型" clearable>
            <el-option v-for="dict in dict.survey_type" :key="dict.dictValue" :label="dict.dictName" :value="dict.dictValue"/>
          </el-select>
        </el-form-item>
  
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8" style="margin-bottom: 10px;">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="plus" @click="handleAdd" >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="delete" :disabled="multiple" @click="handleDelete">删除</el-button>
        </el-col>
      </el-row>
      
      <div class="card-wrapper">
        <el-card class="tb-card" v-for="row in dataList" :key="row.surveyId">
          <template #header>
            <div class="card-header">
              <span style="margin-right: 10px;">{{ row.surveyName }}</span>
              <template v-for="item in dict.survey_type" :key="item.dictValue">
                <el-tag v-if="item.dictValue == row.surveyType" :type="item.cssClass">
                  {{ item.dictName }}
                </el-tag>
              </template>
            </div>
          </template>
          <div class="card-body">
            <p>{{ row.surveyDesc }}</p>
          </div>
          <template #footer>
              <el-button size="small" type="primary" text icon="DocumentAdd" @click="handleDesign(row)">设计</el-button>
              <el-button size="small" type="primary" text icon="View" @click="handlePreview(row)">预览</el-button>
              <!-- <el-button size="small" type="primary" text icon="CopyDocument" @click="handleCopy(row)" >复制</el-button> -->
              <el-button size="small" type="primary" text icon="edit" @click="handleUpdate(row)" >修改</el-button>
              <el-button size="small" type="danger" text icon="delete" @click="handleDelete(row)" >删除</el-button>
          </template>
        </el-card>
      </div>
  
      <el-pagination
        style="margin-top: 10px"
        size="small"
        layout="prev, pager, next"
        :total="total"
        :page-size="queryParams.pageSize"
        :current-page="queryParams.pageNum"
        @current-change="getList"
      />
  
      <!-- 添加或修改问卷对话框 -->
      <el-dialog :title="title" v-model="open" append-to-body>
        <el-form ref="dialogForm" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="模板名称" prop="surveyName">
            <el-input v-model="form.surveyName" placeholder="请输入问卷名称" />
          </el-form-item>
          <el-form-item label="模板类型" prop="surveyType">
            <el-select v-model="form.surveyType" placeholder="请选择模板类型">
              <el-option v-for="dict in dict.survey_type" :key="dict.dictValue" :label="dict.dictName" :value="dict.dictValue"/>
            </el-select>
          </el-form-item>
          <el-form-item label="模板描述" prop="surveyDesc">
            <el-input v-model="form.surveyDesc" type="textarea" placeholder="请输入内容"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import {
    listSurvey,getSurvey,addOrUpdateSurvey,delSurvey,copySurvey
  } from "@/api/survey/survey";
  import { getDicts } from "@/api/dict";
  import { ElMessageBox } from 'element-plus'
  
  export default {
    name: "Template",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 问卷表格数据
        dataList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          surveyName: null,
          surveyType: null,
          status: '1',
          isTemplate: '1',
        },
        // 表单参数
        form: {
          surveyId: null,
          surveyName: null,
          surveyDesc: null,
          surveyType: null,
          isTemplate: '1',
        },
        // 表单校验
        rules: {
          surveyName: [
            { required: true, message: "问卷名称不能为空", trigger: "blur" },
          ],
        },
        dict: {
          survey_type: [],
        },
      };
    },
    async created() {
      this.getList();
      this.dict.survey_type = await getDicts("survey_type");
    },
    methods: {
      /** 查询问卷列表 */
      getList() {
        this.loading = true;
        listSurvey(this.queryParams).then((response) => {
          this.dataList = response.records;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          surveyId: null,
          surveyName: null,
          surveyDesc: null,
          surveyType: null,
          isTemplate: '1',
        };
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.$refs.queryForm.resetFields();
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.surveyId);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加模板";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const surveyId = row.surveyId || this.ids;
        getSurvey(surveyId).then((response) => {
          this.form = response;
          this.open = true;
          this.title = "修改问卷";
        });
      },
      /** 设计按钮操作 */
      handleDesign(row) {
        const surveyId = row.surveyId;
        this.$router.push({
          path: "/surdes/design",
          query: { surveyId: surveyId },
        });
      },
      /** 复制按钮操作 */
      handleCopy(row) {
        const surveyId = row.surveyId;
        copySurvey(surveyId).then(() => {
          this.$message({message: '复制成功',type:'success'});
          this.getList();
        });
      },
      /** 预览按钮操作 */
      handlePreview(row) {
        const surveyId = row.surveyId;
        var routeUrl = this.$router.resolve({
          path: `/preview/${surveyId}`,
        });
        window.open(routeUrl.href, "_blank");
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["dialogForm"].validate((valid) => {
          if (valid) {
            addOrUpdateSurvey(this.form).then((response) => {
              this.$message({message: '修改成功',type:'success'});
              this.open = false;
              this.getList();
            });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const surveyIds = row.surveyId || this.ids;
        ElMessageBox.confirm('是否确认删除问卷编号为" ' + surveyIds + ' "的问卷吗？','警告',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }
        ).then(async () => {
            await delSurvey(surveyIds);
            this.getList();
            this.$message({message: '删除成功',type:'success'});
        }).catch(() => {});
      },
    },
  };
  </script>
  
  <style scoped>
  .card-wrapper {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-gap: 15px;
  }
  .card-body{
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .el-button.el-button--small.is-text{
    padding: 0 4px;
    margin: 0;
  }
  .showcase {
    font-size: 12px;
    vertical-align: middle;
    margin-left: 10px;
  }
  .showcase .el-dropdown-link {
    cursor: pointer;
    color: var(--el-color-primary);
    display: flex;
    align-items: center;
  }
  </style>
  